﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileDB;

namespace Rem.Objects.Forms
{
    public partial class ContractStaticsInfo : BaseForm
    {
        public ContractStaticsInfo()
        {
            InitializeComponent();
            GetInfo();
        }

        public override BaseObjectTypes BaseType
        {
            get
            {
                return BaseObjectTypes.ContractQuery;
            }
        }

        private void GetInfo()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                double getInThisMonth = 0.0;
                double postInThisMonth = 0.0;
                double getInThisYear = 0.0;
                double postInThisYear = 0.0;
                DataView dv =
                Contract.GetAllRentInThisMonth(DateTime.Now.Year, out  getInThisMonth, out  postInThisMonth,
                out  getInThisYear, out postInThisYear);
                this.getInThisMonthText.Text = getInThisMonth.ToString("###,###,##0");
                this.postInThisMonthText.Text = postInThisMonth.ToString("###,###,##0");
                this.getInThisYearText.Text = getInThisYear.ToString("###,###,##0");
                this.postInThisYearText.Text = postInThisYear.ToString("###,###,##0");

                if (dv != null)
                {
                    this.dataGridView1.DataSource = dv;
                    if (dataGridView1.Columns.Count > 0)
                        this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
                    this.dataGridView1.Refresh();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;

            }
        }

        private void 刷新(object sender, EventArgs e)
        {
            GetInfo();

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid == null)
                return;
            if (e.RowIndex < 0 || e.RowIndex > grid.Rows.Count)
            {
                return;
            }
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (e.ColumnIndex == 1)
                {
                    string date = grid.Rows[e.RowIndex].Cells[0].Value.ToString();
                    int year = DateTime.Parse(date).Year;
                    int month = DateTime.Parse(date).Month;
                    double total = double.Parse(grid.Rows[e.RowIndex].Cells[1].Value.ToString());
                    if (total == 0.0)
                    {
                        return;
                    }
                    string text = string.Format("{0}-{1}应收款提醒", year, month);
                    object datasource = Contract.GetReminders(year, month, true);
                    QueryForm f = new QueryForm(BaseObjectTypes.ReminderQuery, text, datasource);

                    f.Dock = DockStyle.Fill;
                    f.Show();
                }
                else if (e.ColumnIndex == 2)
                {
                    string date = grid.Rows[e.RowIndex].Cells[0].Value.ToString();
                    int year = DateTime.Parse(date).Year;
                    int month = DateTime.Parse(date).Month;
                    double total = double.Parse(grid.Rows[e.RowIndex].Cells[2].Value.ToString());
                    if (total == 0.0)
                    {
                        return;
                    }
                    string text = string.Format("{0}-{1}应缴款提醒", year, month);
                    object datasource = Contract.GetReminders(year, month, false);
                    QueryForm f = new QueryForm(BaseObjectTypes.ReminderQuery, text, datasource);
                    f.Dock = DockStyle.Fill;
                    f.Show();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int year = dateTimePicker1.Value.Year;
                double getInThisMonth = 0.0;
                double postInThisMonth = 0.0;
                double getInThisYear = 0.0;
                double postInThisYear = 0.0;
                DataView dv =
                Contract.GetAllRentInThisMonth(year, out  getInThisMonth, out  postInThisMonth,
                out  getInThisYear, out postInThisYear);
                this.textBox2.Text = getInThisYear.ToString("###,###,##0");
                this.textBox1.Text = postInThisYear.ToString("###,###,##0");


                if (dv != null)
                {
                    this.dataGridView2.DataSource = dv;
                    if (dataGridView2.Columns.Count > 0)
                        this.dataGridView2.Sort(this.dataGridView2.Columns[0], ListSortDirection.Ascending);
                    this.dataGridView2.Refresh();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
